#include "qselectmachinedialog.h"
#include "ui_qselectmachinedialog.h"

#include "../libs/core/qbaseitemdelegate.h"

#include <QHeaderView>
#include <QDir>
#include <QFileInfo>
#include <QApplication>
QSelectMachineDialog::QSelectMachineDialog(QWidget *parent) :
    QBaseDialog(parent),
    ui(new Ui::QSelectMachineDialog)
{
    ui->setupUi(this);

    ui->treeWidget->setItemDelegate(new QBaseItemDelegate);
    ui->treeWidget->setRootIsDecorated(false);
    ui->treeWidget->setAlternatingRowColors(true);
    ui->treeWidget->header()->setMovable(false);

    QDir dir(qApp->applicationDirPath()+"/database");

    QList<QFileInfo> list=dir.entryInfoList(QDir::Dirs);

    QString str;
    QStringList slist;
    foreach(QFileInfo info,list)
    {
        str=info.fileName();
        slist=str.split("_");
        slist.removeAll("");
        if(slist.size()==2)
        {
            QTreeWidgetItem *item=new QTreeWidgetItem(ui->treeWidget);
            item->setText(0,slist.first());
            item->setText(1,slist.last());
        }
    }

    connect(ui->treeWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(itemClicked(QTreeWidgetItem*)));
}

QSelectMachineDialog::~QSelectMachineDialog()
{
    delete ui;
}

void QSelectMachineDialog::itemClicked(QTreeWidgetItem *item)
{
    m_type=item->text(0);
    m_version=item->text(1);

    close();
}

QString QSelectMachineDialog::get_type()
{
    return m_type;
}

QString QSelectMachineDialog::get_version()
{
    return m_version;
}
